package com.estate.business.impl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.FileUtils;

import com.estate.business.CompanyStuffService;
import com.estate.dao.CompanyStuffDao;
import com.estate.dao.FileDeleteDao;
import com.estate.utils.AnnouncementUtil;
import com.estate.utils.CompanyStuffUtil;
import com.estate.vo.Vo;
import com.estate.vo.impl.AnnouncementVo;
import com.estate.vo.impl.CompanyStuffVo;
import com.estate.vo.impl.FileDeleteVo;
import com.estate.vo.impl.UserInfoVo;

public class CompanyStuffServiceImpl implements CompanyStuffService {

	private CompanyStuffDao companyStuffDao;
	private FileDeleteDao fileDeleteDao;

	public CompanyStuffDao getCompanyStuffDao() {
		return companyStuffDao;
	}

	public void setCompanyStuffDao(CompanyStuffDao companyStuffDao) {
		this.companyStuffDao = companyStuffDao;
	}

	public FileDeleteDao getFileDeleteDao() {
		return fileDeleteDao;
	}

	public void setFileDeleteDao(FileDeleteDao fileDeleteDao) {
		this.fileDeleteDao = fileDeleteDao;
	}

	public boolean addCompanyStuff(Vo vo,File file) {
		boolean result = false;
		
		CompanyStuffVo cs = (CompanyStuffVo)vo;
		result = this.getCompanyStuffDao().addVo(cs);
		
		if(result){
			this.createFile(cs,file);
		}
		return result;
	}

	public List<Vo> getAllCompanyStuff(UserInfoVo userInfo) {
		List<Vo> csList = new ArrayList<Vo>();
		csList.addAll(this.getCompanyStuffDao().getAllPostedCS());
		csList.addAll(this.getCompanyStuffDao().getMyUnPostedCS(userInfo));
		return csList;
	}

	public Vo getCompanyStuff(int pk) {
		Vo vo = this.getCompanyStuffDao().findByPK(pk);
		return vo;
	}

	public boolean updateCompanyStuff(CompanyStuffVo cs,File file) {
		CompanyStuffVo old = (CompanyStuffVo) this.getCompanyStuffDao().findByPK(cs.getId());
		boolean result = false;
		if(old.getState().equals("未发布")){
			if("已发布".equals(cs.getState())){
				cs.setPostDate(new Date());
				cs.setReadNum(0);
			}
		}else{
			cs.setPostDate(old.getPostDate());
			cs.setReadNum(old.getReadNum());
			
		}
		if(file == null){
			cs.setFilePath(old.getFilePath());
			cs.setFileRealName(old.getFileRealName());
			cs.setFileSaveName(old.getFileSaveName());
			cs.setUploadDate(old.getUploadDate());
			cs.setUploadUser(old.getUploadUser());
		}else{
			FileDeleteVo fileDelete = new FileDeleteVo();
			fileDelete.setFilePath(old.getFilePath());
			this.getFileDeleteDao().addVo(fileDelete);
			
			this.createFile(cs,file);
		}
		cs.setPostUser(old.getPostUser());
		result = this.getCompanyStuffDao().updateVo(cs);
		
		return result;
	}

	public boolean deleteCompanyStuff(Vo vo) {
		boolean result = false;
		CompanyStuffVo cs  = (CompanyStuffVo) this.getCompanyStuffDao().findByPK(((CompanyStuffVo)vo).getId());
		
		FileDeleteVo fileDelete = new FileDeleteVo();
		fileDelete.setFilePath(cs.getFilePath());
		
		this.getCompanyStuffDao().updateVo(cs);
		result = this.getCompanyStuffDao().deleteVo(vo);
		this.getFileDeleteDao().addVo(fileDelete);
		
		return result;
	}

	public boolean deleteMultiCS(String delStr) {
		if(delStr!= null && !"".equals(delStr) && !",".equals(delStr)){
			String[] delArray = delStr.split(",");
			CompanyStuffVo cs = null;
			for(int i = 0;i<delArray.length;i++){
				cs = new CompanyStuffVo();
				cs.setId(Integer.parseInt(delArray[i]));
				this.deleteCompanyStuff(cs);
			}
		}
		return true;
	}
	
	private void createFile(CompanyStuffVo cs,File file){
		if(file != null){
			if(cs.getFilePath() != null && !"".equals(cs.getFilePath())){
				File newFile = new File(cs.getFilePath());
				try {
					FileUtils.copyFile(file, newFile);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public boolean updatePost(int id) {
		CompanyStuffVo old = (CompanyStuffVo) this.getCompanyStuffDao().findByPK(id);
		old.setState(CompanyStuffUtil.getCompanyStuffString(2));
		old.setPostDate(new Date());
		boolean result = this.getCompanyStuffDao().updateVo(old);
		return result;
	}
}
